{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "78beb25a",
   "metadata": {},
   "source": [
    "* 铭晶\n",
    "* week 15\n",
    "* 2021-12-9\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5132bf93",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "> 1.函数的定义 ： def   \n",
    "> 2.函数名的定义规则-->变量名一样的规则    \n",
    "> 3.调用函数的方式    \n",
    "> 4.参数：    \n",
    ">>1.形式参数（定义函数）   \n",
    ">>2.实际参数（调用函数）  \n",
    ">>3.有多个参数时：     \n",
    ">>> 关键字参数     \n",
    ">>> 位置参数（调用函数时）---> 关键字参数要均放在末尾（多个不考虑顺序）    \n",
    ">>> 默认参数（定义函数时）---> 默认参数均放在末尾（多个不考虑顺序）  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18d8ab63",
   "metadata": {},
   "source": [
    "# 本周知识点\n",
    ">1.函数的返回值  \n",
    ">2.函数的输入参数类型   \n",
    ">3.函数的返回参数类型   \n",
    ">4.模块的概念\n",
    ">5.如何创建一个模块，并且调用自己定义的模块"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae5c41ae",
   "metadata": {},
   "source": [
    "## 函数的返回值\n",
    ">1.返回值关键字：return   \n",
    ">>1.函数一定有返回值（无论是什么编程语言）   \n",
    ">>2.在其它的编程语言当中（如c语言，Java等），返回值一定要写，不写会报错！对于Python（更高级的语言），返回值如果不写，给定默认值None   \n",
    ">>3.返回值一般为函数体运行的结果（result）（函数返回值实践1）；也可以是自定义的函数返回值（实践2）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "87c8d7a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数的返回值实践1\n",
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + ' '+ last_name\n",
    "    # print(full_name)  以后在函数中不写print 只写return，print没有意义\n",
    "    # 函数的返回结果为 full_name\n",
    "    return full_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dd6d1634",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'mo ok'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " get_formatted_name(\"mo\",\"ok\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1bae036a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mo ok\n"
     ]
    }
   ],
   "source": [
    "print(get_formatted_name(\"mo\",\"ok\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3affc2b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数的返回值实践2\n",
    "def log_in(username,password):\n",
    "    \"\"\"验证登录信息\"\"\"\n",
    "    if username==\"mj\"and password==123456:\n",
    "        return 1  #  可标注为“登陆成功等”\n",
    "    else:\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7b0d18ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_in(\"mj\",123456)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "95e6c58e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_in(\"mj\",1234560000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c535057",
   "metadata": {},
   "source": [
    "## 函数输入的参数 \n",
    ">1.函数可以输入复杂的参数：   \n",
    ">>1.函数参数为列表    \n",
    ">>2.函数参数为字典     \n",
    "\n",
    ">2.传递任意数量的实参\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c5ddfff7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_A(names):\n",
    "    \"\"\"测试函数输入参数为列表\"\"\"\n",
    "    return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bc834519",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['aaa', 'bbb', 'ccc']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_A([\"aaa\",\"bbb\",\"ccc\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5d5dafac",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_B(person):\n",
    "    \"\"\"测试函数输入参数为字典\"\"\"\n",
    "    return person"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "45515385",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_id': 321, 'age': 18, 'job': 'student'}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_B({\"stud_id\":321,\"age\":18,\"job\":\"student\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db348936",
   "metadata": {},
   "source": [
    "* 2.3的内容\n",
    ">2.函数可以输入不确定个数的参数：   \n",
    ">>1. *args    \n",
    ">>>1. args可以接受多个数据（单值）     \n",
    ">>>2. args将多个数据存在一个**元组**当中，实际还是一个变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "04c486e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def food_ingred(food,*ingreds):\n",
    "    \"\"\"食物配料单\"\"\"\n",
    "    return food,ingreds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "1822d8ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('noodle', ('tomato', 'beef', 'pig'))"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "food_ingred(\"noodle\",\"tomato\",\"beef\",\"pig\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "90a45429",
   "metadata": {},
   "outputs": [],
   "source": [
    "def person(stud_name,**stud_course):\n",
    "    \"\"\"学生个人信息完善表\"\"\"\n",
    "    return stud_name,stud_course"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "9d9559a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('aaa', {'course_周一': 'english', 'course_周四': 'Python'})"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person(\"aaa\",course_周一=\"english\",course_周四=\"Python\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "227e7d21",
   "metadata": {},
   "source": [
    "## 函数的输出（返回值）参数类型\n",
    "\n",
    ">1.在编程语言中，函数的返回值只能是一个值    \n",
    ">>2.返回值可以是字符串、数值、列表、字典等数据（单一值，只能是一个值）   \n",
    ">>3.python可以输出多个返回值，结果会用一个元组进行封装并返回。（本质不违背函数的定义）\n",
    "\n",
    ">2.函数可以输入不确定个数的参数：    \n",
    ">>1. *args   \n",
    ">>2. *kwargs\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "be1e9a9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test1_return():\n",
    "    \"\"\"测试返回值return为数值\"\"\"\n",
    "    return 12345"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "579325ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12345"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test1_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "9cc0ada1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test2_return():\n",
    "    \"\"\"测试返回值return为字符串\"\"\"\n",
    "    return \"你好\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "edf3d5ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test2_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "1becdf55",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test3_return():\n",
    "    \"\"\"测试返回值return为列表\"\"\"\n",
    "    return ['aaa','bbb','ccc']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "1c25dbc8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['aaa', 'bbb', 'ccc']"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test3_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "ee864ab3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test4_return():\n",
    "    \"\"\"测试返回值return为字典\"\"\"\n",
    "    return {\"stud_id\":321,\"age\":18,\"job\":\"student\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "2834aa0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_id': 321, 'age': 18, 'job': 'student'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test4_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "b6f3b9bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "#   *******************************return 后面写多个值，将会返回一个元组，（一个值，不违背函数在编程思维中的定义）\n",
    "def test5_return():\n",
    "    \"\"\"测试返回值return为 多个值的情况\"\"\"\n",
    "    return 18,\"aaa\",{\"stud_id\":321,\"age\":18,\"job\":\"student\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "95fe1d58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(18, 'aaa', {'stud_id': 321, 'age': 18, 'job': 'student'})"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test5_return()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "855cea5c",
   "metadata": {},
   "source": [
    "## 4.模块\n",
    ">1.使用模块可以隐藏函数的源代码   \n",
    ">2.使用的人不需要会底层代码（只需要会使用模块即可） \n",
    "\n",
    ">3.调用模块的方法   \n",
    ">>1.必须先调用模块，才可以使用\n",
    ">>> *使用\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "b811bb79",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数的定义\n",
    "# def search4vowels(word):\n",
    "   #  \"\"\"返回文本中出现的元音字母\"\"\"\n",
    "    # return set(\"aeiou\")&set(word)\n",
    "#  ***将该框的代码剪切，在week15中新建一个text文件，改名study后缀为py,重启内核运行即可，写import study，即可***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a4f7c663",
   "metadata": {},
   "outputs": [],
   "source": [
    "import study"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f9340bf9",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'search4vowels' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-14-d1476b80d906>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 函数的调用\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0msearch4vowels\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"hello xiaoming\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'search4vowels' is not defined"
     ]
    }
   ],
   "source": [
    " # 函数的调用\n",
    "search4vowels(\"hello xiaoming\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c4681979",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'study' has no attribute 'ws_链接'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-15-762312669143>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstudy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mws_链接\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'https://www.nfu.edu.cn/'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'xydt'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'96'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: module 'study' has no attribute 'ws_链接'"
     ]
    }
   ],
   "source": [
    "study.ws_链接('https://www.nfu.edu.cn/','xydt','96')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e792eeed",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
